package com.sangdaelee.callme.dialog;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockDialogFragment;
import com.sangdaelee.callme.GroupFragment;
import com.sangdaelee.callme.ListData;
import com.sangdaelee.callme.R;
import com.sangdaelee.callme.database.DBModel;

@SuppressLint("ValidFragment")
public class DetailContactDialog extends SherlockDialogFragment {
	
	private Cursor c;
	private String id;
	private Button btn;
	private ListData picData;
	
	public DetailContactDialog(long id) {
		this.id = String.valueOf(id);
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		LayoutInflater inflater = getActivity().getLayoutInflater();
		final View customView = inflater.inflate(R.layout.custom_dialog_detail_contact_data, null);
		TextView name = (TextView)customView.findViewById(R.id.detailName);
		TextView number = (TextView)customView.findViewById(R.id.detailNumber);
		TextView group = (TextView)customView.findViewById(R.id.detailGroup);
		TextView receive = (TextView)customView.findViewById(R.id.receiveCount);
		TextView send = (TextView)customView.findViewById(R.id.sendCount);
		ImageView iv = (ImageView)customView.findViewById(R.id.contactImage);
		
		picData = new ListData();
		c = DBModel.getInstance().getDetailContactData(id);
		
		Bitmap photo = picData.get_bitmap(c);
		
		String receiveCount = String.valueOf(c.getLong(5));
		String sendCount = String.valueOf(c.getLong(6));
		
		iv.setImageBitmap(photo);
		
		btn = (Button)customView.findViewById(R.id.callingBtn);
		
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Uri uri = Uri.parse("tel:"+c.getString(2));
				Intent it = new Intent(Intent.ACTION_DIAL, uri);  
				startActivity(it);
				dismiss();
			}
		});
		
		btn = (Button)customView.findViewById(R.id.mailBtn);
		
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Uri uri = Uri.parse("smsto:"+c.getString(2));   
				Intent it = new Intent(Intent.ACTION_SENDTO, uri);   
//				it.putExtra("sms_body", "The SMS text");   
				startActivity(it);
				dismiss();
			}
		});
		
		btn = (Button)customView.findViewById(R.id.cancelBtn);
		
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dismiss();
			}
		});
		
		name.setText(c.getString(3));
		number.setText(c.getString(2));
		group.setText(c.getString(8));
		receive.setText(receiveCount);
		send.setText(sendCount);
		
		return new AlertDialog.Builder(getActivity()).setView(customView).create();
	}
	
	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub
		
		if(c != null)
		{
			c.close();
		}
		super.onDestroyView();
	}
}
